package com.feiyu.common.handler;

import com.feiyu.common.exception.GlobalException;
import org.springblade.core.tool.api.R;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 统一异常处理
 * @author tang ming
 * @since 2021/12/7 10:39
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public R exceptionHandler(Exception e){
        e.printStackTrace();
        if (e instanceof GlobalException){
            GlobalException ge= (GlobalException) e;
            return R.fail(ge.getResultCode());
        }
        return R.fail(e.getMessage());
    }
}
